//
//  ViewController.m
//  02-图片折叠
//
//  Created by xiaomage on 15/9/29.
//  Copyright (c) 2015年 xiaomage. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *topImageV;

@property (weak, nonatomic) IBOutlet UIImageView *bottomImageV;
@property (weak, nonatomic) IBOutlet UIView *dragView;
@property (nonatomic, weak)  CAGradientLayer *gradient;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    //让上部图片只显示上半部分
    //只显示图层当中的某一部分内容
    self.topImageV.layer.contentsRect = CGRectMake(0, 0, 1, 0.5);
    self.topImageV.layer.anchorPoint = CGPointMake(0.5, 1);
    
    //让下部图片只显示下半部分
    self.bottomImageV.layer.contentsRect = CGRectMake(0, 0.5, 1, 0.5);
    self.bottomImageV.layer.anchorPoint = CGPointMake(0.5, 0);
    
    //添加手势
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc ] initWithTarget:self action:@selector(pan:)];
    [self.dragView addGestureRecognizer:pan];
    
    //渐变层
    CAGradientLayer *gradient = [CAGradientLayer layer];
    gradient.frame = self.bottomImageV.bounds;
    //设置渐变的颜色
    gradient.colors = @[(id)[UIColor clearColor].CGColor,(id)[UIColor blackColor].CGColor];
    
    //设置不透明度
    gradient.opacity = 0;
    self.gradient = gradient;
    
    [self.bottomImageV.layer addSublayer:gradient];

}



- (void)gradientLayer{
    
    //渐变层
    CAGradientLayer *gradient = [CAGradientLayer layer];
    gradient.frame = self.bottomImageV.bounds;
    //设置渐变的颜色
    gradient.colors = @[(id)[UIColor redColor].CGColor,(id)[UIColor greenColor].CGColor,(id)[UIColor blueColor].CGColor];
    //设置渐变的方向
    gradient.startPoint = CGPointMake(0, 0);
    gradient.endPoint = CGPointMake(1, 0);
    //从哪个点开始渐变到下一个颜色
    gradient.locations = @[@(0.5),@(0.8)];

}

- (void)pan:(UIPanGestureRecognizer *)pan{

    //获取当前手指的偏移量
    CGPoint transP = [pan translationInView:self.dragView];
  
    //最大旋转180.0;
    //当手指偏移量为200是180度
    CGFloat angle = transP.y * M_PI / 200.0;
    
    //设置立体效果,(近大远小的效果)
    CATransform3D transform = CATransform3DIdentity;
    //眼睛离屏幕的距离
    transform.m34 = -1 / 300.0;
    
    //设置渐变层的不透明度
    self.gradient.opacity = transP.y * 1 / 200.0;
    //让上部图片绕着X轴做旋转.
    self.topImageV.layer.transform = CATransform3DRotate(transform, -angle, 1, 0, 0);
    
    
    //当手指松开时,上部图片复位.
    if(pan.state == UIGestureRecognizerStateEnded){
        //把渐变变成透明的效果
        self.gradient.opacity = 0;
      
        //delay:动画延时执行时间
        //Damping:弹性系数,设的超小, 弹性就越大
        //Velocity:弹性的初始速度
        //options:以什么样样式来做动画,开始有结束慢,
        [UIView animateWithDuration:1 delay:0 usingSpringWithDamping:0.1 initialSpringVelocity:0 options:UIViewAnimationOptionCurveLinear animations:^{
            
            //让上部图片复位.
            self.topImageV.layer.transform = CATransform3DIdentity;
            
        } completion:nil];
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
